from PIL import Image, ImageDraw, ImageFont
import random

# 随机生成图片大小
width = random.randint(500, 1000)
height = random.randint(500, 1000)

# 创建空白图片
image = Image.new("RGB", (width, height), (255, 255, 255))

# 创建一个可以在图片上绘图的对象
draw = ImageDraw.Draw(image)

# 随机生成背景颜色
bg_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
draw.rectangle((0, 0, width, height), fill=bg_color)

# 加载中文字体
font_path = "simsun.ttc"  # 中文字体文件路径
font_size = random.randint(20, 60)
font = ImageFont.truetype(font_path, font_size)

# 随机生成文本内容
text = "图片严重违规，请重新上传！"
for i in range(random.randint(1, 5)):
    text += chr(random.randint(0x4e00, 0x9fa5))
    # chr 函数将Unicode码转换成对应的字符

# 随机生成文本颜色
text_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

# 计算文本位置
text_width, text_height = draw.textsize(text, font=font)
x = random.randint(0, width - text_width)
y = random.randint(0, height - text_height)

# 在图片上添加文本
draw.text((x, y), text, font=font, fill=text_color)

# 保存随机生成的图片
image.save("random_image.jpg")









